<html>
<head><meta charset="utf-8"><title>Revisiting default struct field values · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html">Revisiting default struct field values</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214240254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214240254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214240254">(Oct 22 2020 at 19:56)</a>:</h4>
<p>I've been against this in the past, but threads like <a href="https://internals.rust-lang.org/t/pre-pre-rfc-syntactic-sugar-for-default-default/13234?u=scottmcm">https://internals.rust-lang.org/t/pre-pre-rfc-syntactic-sugar-for-default-default/13234?u=scottmcm</a> have been changing my opinions about it.</p>
<p>Looks like there was some potential interest in the past, but the RFC was postponed for the 2017 impl period: <a href="https://github.com/rust-lang/rfcs/pull/1806#issuecomment-327922562">https://github.com/rust-lang/rfcs/pull/1806#issuecomment-327922562</a></p>
<p>How are people's temperature about that kind of change these days?  I feel like all the <code>const</code> improvements we've had since then help answer some of the things that were previously unknowns.</p>



<a name="214240453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214240453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214240453">(Oct 22 2020 at 19:57)</a>:</h4>
<p>I'm not opposed but the <code>..*</code> syntax etc seems unnecessary to me</p>



<a name="214240976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214240976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214240976">(Oct 22 2020 at 20:01)</a>:</h4>
<p>Can you elaborate on the <code>*</code> there, <span class="user-mention" data-user-id="116009">@nikomatsakis</span>?  Do you mean "<code>..</code> is sufficient" or "<code>Foo { a, b }</code> should always do this" or something else?</p>



<a name="214242193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214242193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214242193">(Oct 22 2020 at 20:11)</a>:</h4>
<p>I meant that I would expect to just write <code>Foo { a, b }</code></p>



<a name="214242750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214242750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214242750">(Oct 22 2020 at 20:16)</a>:</h4>
<p>I like having the <code>..</code> as a mirror to the pattern syntax</p>



<a name="214242946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214242946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214242946">(Oct 22 2020 at 20:18)</a>:</h4>
<p>having _nothing_ seems like bad style, at least, akin to <code>elided-lifetimes-in-paths</code></p>



<a name="214244394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214244394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214244394">(Oct 22 2020 at 20:32)</a>:</h4>
<p>I think of it more like default items in traits</p>



<a name="214244689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214244689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214244689">(Oct 22 2020 at 20:35)</a>:</h4>
<p>elided lifetimes in paths feels different, my motivation there was always that seeing <code>-&gt; Foo&lt;'_&gt;</code> (and similarly in other places) tells me that this is a value that is tied to the stack frame</p>



<a name="214245063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214245063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214245063">(Oct 22 2020 at 20:38)</a>:</h4>
<p>it seems important to me to indicate that the size of the thing I created is more than just <code>a</code>+<code>b</code></p>



<a name="214248912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214248912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214248912">(Oct 22 2020 at 21:12)</a>:</h4>
<p>I'm not sure if "size" is what I care about, but I agree it seems important to indicate there's "more to it" -- at least, in general. It would seem pretty surprising to me that e.g. <code>Foo { }</code> can create a struct with fields</p>



<a name="214249118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214249118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214249118">(Oct 22 2020 at 21:15)</a>:</h4>
<p>Another note is that presumably even the sugar would still require all fields to be public/visible which would be even more confusing diagnostic wise if we can't even point at something that is standing in place of those fields, I imagine</p>



<a name="214250971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214250971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214250971">(Oct 22 2020 at 21:33)</a>:</h4>
<p>I imagine we would point at the struct name and say "struct has private fields" or something</p>



<a name="214251006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214251006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214251006">(Oct 22 2020 at 21:33)</a>:</h4>
<p>I guess it'd be good to discuss the use cases, I was imagining it was avoiding verbosity and (maybe) some amount of forward compatible evolution -- I guess it doesn't achieve that because of pattern destructuring</p>



<a name="214260348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214260348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214260348">(Oct 22 2020 at 23:38)</a>:</h4>
<p>Yeah, I think the pattern precedent controls here.  If the difference of <code>Foo { a, b }</code> vs <code>Foo { a, b, .. }</code> is valuable in a pattern, then it implies to me that it's valuable in an expression too.</p>
<p>(And ideally this would work like patterns already do for <code>non_exhaustive</code> structs: inside the crate you can omit the <code>..</code> when creating it, but if it's an external crate you need to have the <code>..</code> for forward compatibility with field additions.  Though that does need some sort of <code>non_exhaustive(new-fields-will-always-have-defaults)</code> opt-in.)</p>



<a name="214263126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214263126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214263126">(Oct 23 2020 at 00:31)</a>:</h4>
<p>i would love a shorthand here but i also wouldn't want to lose the breaking change i get if i add a field to a struct thats typically instantiated in full. really helpful when refactoring. e.g. <code>Foo { a, b, .. }</code> <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span>, but <code>Foo { a, b }</code> <span aria-label="-1" class="emoji emoji-1f44e" role="img" title="-1">:-1:</span> (if foo has more than 2 fields).</p>



<a name="214269533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214269533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214269533">(Oct 23 2020 at 03:12)</a>:</h4>
<p>That makes me think of writing wrappers for things with default parameters in C#, actually.  It's really easy to forget to pass one (or more) of the parameters that I was intending to.  I guess in that case the opt-in to getting the defaults would be suboptimal in many cases where it's actually used, but that's pretty different from how <code>struct</code>s are used, I think.</p>
<p>But I suppose we could also talk about moving to an opt-out model for it.  <code>Foo { a, b, 🚲🏠 }</code>, or whatever.</p>



<a name="214269909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214269909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214269909">(Oct 23 2020 at 03:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values/near/214269533">said</a>:</p>
<blockquote>
<p><code>Foo { a, b, 🚲🏠 }</code></p>
</blockquote>
<p><span aria-label="big smile" class="emoji emoji-1f604" role="img" title="big smile">:big_smile:</span></p>



<a name="214270569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214270569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214270569">(Oct 23 2020 at 03:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values/near/214242193">said</a>:</p>
<blockquote>
<p>I meant that I would expect to just write <code>Foo { a, b }</code></p>
</blockquote>
<p>In addition to the comment that this would not match the requirement for patterns, I'd also say that it's important to me to have to update anywhere that said "only initialize a and b" if there are more fields than just a and b. I should have to acknowledge "and other things that I don't care about".</p>



<a name="214270589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Revisiting%20default%20struct%20field%20values/near/214270589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values.html#214270589">(Oct 23 2020 at 03:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/213817-t-lang/topic/Revisiting.20default.20struct.20field.20values/near/214263126">said</a>:</p>
<blockquote>
<p>i would love a shorthand here but i also wouldn't want to lose the breaking change i get if i add a field to a struct thats typically instantiated in full. really helpful when refactoring. e.g. <code>Foo { a, b, .. }</code> <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span>, but <code>Foo { a, b }</code> <span aria-label="-1" class="emoji emoji-1f44e" role="img" title="-1">:-1:</span> (if foo has more than 2 fields).</p>
</blockquote>
<p>This, precisely. Add a field, let the compiler tell you everywhere that's broken.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>